'''
Author: Hugh
Date: 2022-09-06 21:08:41
LastEditors: tongyi hughtong@outlook.com
LastEditTime: 2023-05-25 19:02:03
FilePath: \TISAConsoleCtrl\crc.py
Description: 

Copyright (c) 2022 by All/Terapark, All Rights Reserved. 
'''


from cgi import test
# from pycrc import 

class crc:
    # ls is a list type data, invert is a bool type data
    @classmethod
    def crc16(self, ls, invert):
        a = 0xFFFF
        b = 0xA001
        for byte in ls:
            a ^= byte
            for i in range(8):
                last = a % 2
                a >>= 1
                if last == 1:
                    a ^= b
        s = '0x%04x' % a
        return list(bytes.fromhex(s[4:6]+s[2:4])) if invert == True else list(bytes.fromhex(s[2:4]+s[4:6]))

#  test
if __name__ == "__main__":
    ls = crc.crc16([0x01, 0x04, 0x00, 0x00, 0x00, 0x02], True)
    print(bytes(ls).hex(" "))


    print(crc.crc16([0x01, 0x4e, 0x00, 0xc8], False))
    print([0x01, 0x4e, 0x00, 0xc8])
    str = '6059'
    by = bytes.fromhex(str)
    print(by[0], by[1])
